﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcOrganizer.Filters;
using MvcOrganizer.Models;
using WebMatrix.WebData;
using MvcOrganizer.ViewModels;
using System.Web.Security;
using System.Data;

namespace MvcOrganizer.Controllers
{
    [Authorize(Roles = "Administrator")]
    [InitializeSimpleMembership]
    public class AdminController : Controller
    {
        private Organizer db = new Organizer();

        public ActionResult Index()
        {
            var viewmodel = new AdminViewModel
            {
                reports = db.Reports.ToList(),
                users = db.UserProfiles.ToList()
            };
            return View(viewmodel);
        }

        public ActionResult EditReport(int id = 0)
        {
            Report report = db.Reports.Find(id);
            if (report == null)
            {
                return HttpNotFound();
            }
            return View(report);
        }

        [HttpPost]
        public ActionResult EditReport(Report report)
        {
            if (ModelState.IsValid)
            {
                db.Entry(report).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(report);
        }

        public ActionResult EditUser(int id = 0)
        {
            UserProfile user = db.UserProfiles.Find(id);
            if (user == null)
            {
                return HttpNotFound();
            }
            return View(user);
        }

        [HttpPost]
        public ActionResult EditUser(UserProfile user)
        {
            if (ModelState.IsValid)
            {
                db.Entry(user).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(user);
        }

        public ActionResult DeleteReport(int id = 0)
        {
            Report report = db.Reports.Find(id);
            if (report == null)
            {
                return HttpNotFound();
            }
            return View(report);            
        }

        [HttpPost, ActionName("DeleteReport")]
        public ActionResult DeleteReportConfirmed(int id)
        {
            Report report = db.Reports.Find(id);
            db.Reports.Remove(report);
            db.SaveChanges();
            return RedirectToAction("Index");            
        }

        public ActionResult DeleteUser(int id = 0)
        {
            UserProfile user = db.UserProfiles.Find(id);
            if (user == null)
            {
                return HttpNotFound();
            }
            return View(user);          
        }

        [HttpPost, ActionName("DeleteUser")]
        public ActionResult DeleteUserConfirmed(int id)
        {
            UserProfile user = db.UserProfiles.Find(id);
            db.UserProfiles.Remove(user);
            db.SaveChanges();
            return RedirectToAction("Index");           
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
